%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% createMissingDirs.m
% generateOutputDirectory.m
%
%==========================================================================

%==========================================================================
% The function "setUpProjectDirectories" sets and creates the output and
% scratch directories for an existing project already populated with the
% elements of an input file. It also sets a project file name and file name
% prefix for converted image files if any are to be generated later on.
% INPUT: "projectStruct" is a structure whose fields store various
% information relevant to optical sound extraction.
% OUTPUT: "projectStruct" returns an updated version of the structure
% received as an input parameter.
%==========================================================================
function projectStruct = setUpProjectDirectories(projectStruct)

% Check if the given project stucture is sufficiently populated.
if ( (isfield(projectStruct, 'inFile') == false)|| ...
        (isfield(projectStruct.inFile, 'namePrefixNoPath') == false)|| ...
        (isfield(projectStruct.inFile, 'dirPath') == false)|| ...
        (isfield(projectStruct, 'projNamePrefixNoPath') == false)|| ...
        (isfield(projectStruct, 'projIndFieldLength') == false)|| ...
        (isfield(projectStruct, 'scratchDir1') == false)|| ...
        (isfield(projectStruct, 'scratchDir2') == false) )
        
    error('SoundRestoration:setUpProjectDirectories:GivenProjectStructureIsIncomplete', ...
        'The given project structure is incomplete. Some necessary fields are missing.');
end
        
% Get the file name prefix (excluding the directory path) of the specified
% template less one underscore at its very end, if such is present.
if (strcmp(projectStruct.inFile.namePrefixNoPath(end), '_') == false)
    localDirName = projectStruct.inFile.namePrefixNoPath(1:end);
else
    localDirName = projectStruct.inFile.namePrefixNoPath(1:(end - 1));
end

%==========================================================================
% Set up the output directory (including a full directory path). Note that
% at any moment there are two strings refering to output directory: the
% field "projectStruct.outputDir" corresponds to the content of the GUI
% uicontrol "outputDir", while the field "projectStruct.outFile.outputDir"
% stores the output directory, where the output files are actually written.
% The latter is two directory deeper then the former, the two extra
% subdirectories being one specific for the video source with another
% subdirectory specific to the project which processes that video source.
%==========================================================================
% If a valid output directory has not been specified in the GUI, then use
% the directory where the given input template resides.
if ( (isfield(projectStruct, 'outputDir') == false)|| ...
        (isempty(projectStruct.outputDir) == true)|| ...
        (exist(projectStruct.outputDir, 'dir') ~= 7) )
    projectStruct.outputDir = projectStruct.inFile.dirPath;
end
% Generate the name of the directory where output is going to actually be
% written.
projectStruct.outFile.outputDir = generateOutputDirectory([projectStruct.outputDir ...
    upper(localDirName(1)) localDirName(2:end) filesep], localDirName, ...
    projectStruct.projNamePrefixNoPath, projectStruct.projIndFieldLength);

% Get the name of the project-specific, local directory.
[~, outputDirNameNoPath] = fileparts(projectStruct.outFile.outputDir(1:(end - 1)));
% Construct a name for a project file for the current project.
projectStruct.outFile.projectFileName = [projectStruct.outFile.outputDir ...
    lower(outputDirNameNoPath(1)) outputDirNameNoPath(2:end) '.mat'];
    
% Set the scratch directories (including a full directory path). The first
% will store any converted image files that may be used in other projects
% too. It can not be cleaned from within the GUI. The second will store all
% temporary, project-specific files, and it is possible to clean it from
% within the GUI.
projectStruct.outFile.scratchDir1 = [projectStruct.scratchDir1 upper(localDirName(1)) localDirName(2:end) filesep];
projectStruct.outFile.scratchDir2 = [projectStruct.scratchDir2 outputDirNameNoPath filesep];

% Construct a file name prefix for the new image files where frames are to
% be stored.
projectStruct.workFile.namePrefix = [projectStruct.outFile.scratchDir1 localDirName '_'];

% Create any missing directories.
createMissingDirs(projectStruct.outFile.outputDir);
createMissingDirs(projectStruct.outFile.scratchDir1);
createMissingDirs(projectStruct.outFile.scratchDir2);

return;
% end of the function "setUpProjectDirectories"
